﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using HtmlAgilityPack;
using FinanceGoldPointDal.DTO.Filters;
using System.Globalization;
using FinanceGoldPointDal.DAL;
using FinanceGoldPointDal.DTO;

namespace FinanceGoldPointDal.Utils
{
  public class BaseStockUtil : baseDAL
  {

    #region Download
   
    //DOWMLOAD web page
    public RetData<string> DownloadWebString(string fullQuoteUrl)
    {
      try
      {
        using (var wc = new WebClient())
        {
          return new RetData<string>( wc.DownloadString(new Uri(fullQuoteUrl)) );
        }
      }
      catch (Exception ex)
      {
        return new RetData<string>(ex);
      }
    }//fM

    #endregion
    //*************************************************************************************
  

    #region DOM DOCUMENT


    protected HtmlDocument getDomDocument(string file)
    {
      var doc = new HtmlDocument();
      doc.LoadHtml(file);
      return doc;
    }


    internal List<HtmlNode> getTablesInDomDocument(HtmlDocument pDoc, string pWithSpecificAttrinuteName)
    {
      var tables =  pDoc.DocumentNode.SelectNodes("//table").ToList();
      if (!string.IsNullOrEmpty(pWithSpecificAttrinuteName))
      {
        tables = tables.Where(xx => xx.Attributes.Contains(pWithSpecificAttrinuteName)).ToList();
      }
      
      return tables;
    }

    internal List<HtmlNode> getTRinTablesDom(HtmlNode pTableNode, FilterDom pFilter)
    {
      var trs = pTableNode.SelectNodes("//tr").ToList();
      if (pFilter != null)
      {
        trs = trs.Where(xx => 
          
           ( string.IsNullOrEmpty( pFilter.WithSpecificAttributeName ) ? true : xx.Attributes.Contains(pFilter.WithSpecificAttributeName ) )
          & (string.IsNullOrEmpty(pFilter.WithSpecificAttributeValue) ? true : 
              xx.Attributes.Select( pp=> pp.Value.Equals( pFilter.WithSpecificAttributeValue)  ).Count() > 0 )
          & (string.IsNullOrEmpty(pFilter.WithSpecificText) ? true : xx.InnerText.Contains(pFilter.WithSpecificText))
          
          ).ToList();
      }

      return trs;
    }

    internal List<HtmlNode> getTDsInTablesDom(HtmlNode pTRNode, FilterDom pFilter)
    {
      var tds = pTRNode.SelectNodes("//td").ToList();
      if (pFilter != null)
      {
        tds = tds.Where(xx =>

           (string.IsNullOrEmpty(pFilter.WithSpecificAttributeName) ? true : xx.Attributes.Contains(pFilter.WithSpecificAttributeName))
          & (string.IsNullOrEmpty(pFilter.WithSpecificAttributeValue) ? true :
              xx.Attributes.Select(pp => pp.Value.Equals(pFilter.WithSpecificAttributeValue)).Count() > 0)
          & (string.IsNullOrEmpty(pFilter.WithSpecificText) ? true : xx.InnerText == pFilter.WithSpecificText)

          ).ToList();
      }

      return tds;
    }

    internal List<HtmlNode> getTDsInTablesDom(HtmlNode pTRNode, string pWithSpecificAttributeName, string pWithSpecificAttributeValue, string pWithSpecificText)
    {
      var tds = pTRNode.SelectNodes("//td").ToList();

        tds = tds.Where(xx =>

           (string.IsNullOrEmpty(pWithSpecificAttributeName) ? true : xx.Attributes.Contains(pWithSpecificAttributeName))
          & (string.IsNullOrEmpty(pWithSpecificAttributeValue) ? true :
              xx.Attributes.Select(pp => pp.Value.Equals(pWithSpecificAttributeValue)).Count() > 0)
          & (string.IsNullOrEmpty(pWithSpecificText) ? true : xx.InnerText.StartsWith(pWithSpecificText))

          ).ToList();
      

      return tds;
    }

    

    #endregion
    //*************************************************************************************

    #region Conversioni dati

    internal double? getDoubleNullValFromGB(string pIn)
    {
      double dd = 0;

      var style = NumberStyles.Number | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.Any;
      var culture = CultureInfo.CreateSpecificCulture("en-GB");
      //culture = CultureInfo.CreateSpecificCulture("it-IT");

      if (double.TryParse(pIn, style, culture, out dd))
        return dd;
      else
        return null;
    }


    internal string bonificaCifraDaDefMigliaia(string pIn)
    {
      if (pIn.EndsWith("mld"))
        return (pIn.Substring(0, pIn.Length - 3)+ "0,000,000");

      return "";
    }


    internal double? getDoubleNullValFromPercentual(string pIn)
    {
      double dd = 0;

      //var style = NumberStyles.Number | NumberStyles.AllowDecimalPoint;
      //var culture = CultureInfo.CreateSpecificCulture("en-GB");

      if (double.TryParse(pIn,  out dd))
        return dd;
      else
        return null;
    }

    #endregion
    //**************************************************************************************

    #region Manipolazioni Dati

    public string[] splitQuoteInArray(string pDatiQuota)
    {
      // clear out quote marks - don't want
      var rawData = pDatiQuota.Replace("\"", "");
      return rawData.Split(new char[] { '\r', '\n' });
    }

    internal string TroncaNUltimiCaratteri(string pDato,int pTotCar)
    {
      return pDato.Substring(0, pDato.Length - pTotCar);
    }


    #endregion


  }
}
